//
//  AppDelegate.swift
//  SinaImiate
//
//  Created by xiaoming on 16/6/29.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
       setupAdditions()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white()
        window?.rootViewController = SIMainController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }

    
}

extension AppDelegate {
    
    private func setupAdditions() {
        
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        ///用户授权
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.alert, .badge, .sound]) { (auth, error) in
                print("授权" + (auth ? "成功" : "失败"))
            }
        } else {
            
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared().registerUserNotificationSettings(notifySettings)
        }
        
        
    }
}

extension AppDelegate {
    
    //MARK: -模拟异步加载  保存在沙盒
    private func loadAppInfo() {
        
        DispatchQueue.global().async { 
            
            let url = Bundle.main.urlForResource("demo.json", withExtension: nil)
            
            let data = NSData(contentsOf: url!)
            
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用程序加载完毕 \(jsonPath)")
            
            
        }
        
    }
    
}
